package com.chenju.javastatic.controller;

import com.chenju.javastatic.common.exceptions.EasyFileNotFoundException;
import com.chenju.javastatic.domain.pojo.DiffMethodsListPageRequestPoJo;
import com.chenju.javastatic.common.response.ResponseEntity;
import com.chenju.javastatic.domain.pojo.ParseCodeDiffPageRequestPoJo;
import com.chenju.javastatic.domain.pojo.ParsedRecordListPageRequestPoJo;
import com.chenju.javastatic.service.JavaParserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileNotFoundException;

@RestController
@RequestMapping(value = "/javaparser")
public class JavaParserController {
    @Autowired
    private JavaParserService javaParserService;

    @PostMapping(value = "/getDiffMethods")
    public ResponseEntity getDiffMethods(@RequestBody DiffMethodsListPageRequestPoJo diffMethodsListPageRequestPoJo){
        return ResponseEntity.success(javaParserService.selectAllDiffMethods(diffMethodsListPageRequestPoJo));
    }

    @PostMapping(value = "/parseCommitDiff")
    public ResponseEntity parseCommitDiff(){
        File repoDir = new File("D:\\Project\\springboot-easytest-simple");
        String oldCommitName = "ee3bf2b";
        String newCommitName = "528f416";
        javaParserService.parseCommitDiff("", repoDir, oldCommitName, newCommitName);

        return ResponseEntity.success("操作成功");
    }

    @PostMapping(value = "/parseCodeDiff")
    public ResponseEntity parseCodeDiff(@RequestBody ParseCodeDiffPageRequestPoJo parseCodeDiffPageRequestPoJo){
        if (parseCodeDiffPageRequestPoJo.getRepoDir() == null || "".equals(parseCodeDiffPageRequestPoJo.getRepoDir()) || !new File(parseCodeDiffPageRequestPoJo.getRepoDir()).exists()){
            return ResponseEntity.fail("仓库路径不能为空或目录不存在");
        }
        if (parseCodeDiffPageRequestPoJo.getOldBranch() == null || "".equals(parseCodeDiffPageRequestPoJo.getOldBranch())){
            return ResponseEntity.fail("旧分支名称不能为空");
        }
        if (parseCodeDiffPageRequestPoJo.getNewBranch() == null || "".equals(parseCodeDiffPageRequestPoJo.getNewBranch())){
            return ResponseEntity.fail("新分支名称不能为空");
        }
        File repoDir = new File(parseCodeDiffPageRequestPoJo.getRepoDir());
        javaParserService.parseCommitDiff(parseCodeDiffPageRequestPoJo.getProjectName(), repoDir, parseCodeDiffPageRequestPoJo.getOldBranch(), parseCodeDiffPageRequestPoJo.getNewBranch());
        return ResponseEntity.success();
    }

    @GetMapping(value = "/getDiffMethodById")
    public ResponseEntity getDiffMethodById(@RequestParam Long id){
        try{
            return ResponseEntity.success(javaParserService.selectDiffMethodById(id));
        } catch (Exception e){
            return ResponseEntity.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/getParsedList")
    public ResponseEntity getParsedList(@RequestBody ParseCodeDiffPageRequestPoJo parsedRecordListPageRequestPoJo){
        return ResponseEntity.success(javaParserService.selectAllParsedRecords(parsedRecordListPageRequestPoJo));
    }

    @DeleteMapping(value = "/deleteRecordById")
    public ResponseEntity deleteRecordById(@RequestParam Long id){
        javaParserService.deleteRecordById(id);
        return ResponseEntity.success();
    }
}
